function extractPolygonsPoints (svg) {
    var localtion = svg.contentDocument.getElementById("localtion");
    // console.log("localtion : ",localtion);
    var polygonsPoints = getPolygonPoints(localtion.childNodes);
    // console.log("polygons : ",polygonsPoints);
    return polygonsPoints;
}

function getPolygonPoints (nodes) {
    var result = new Object();
    for (let i=0;i<nodes.length;i++){
        var id = nodes[i].id;  
        var newId;

        if (id && typeof(id) != undefined) {
            var reg = RegExp(/-/);
            if (id.match(reg)) {
                var tt = id.split('-');
                newId = tt[0];
            } else {
                newId =id;
            }
            // console.log("id :",id);
            var d = nodes[i].attributes.d.value;
            var points = extractPoints(d);
            var transformPoints;
            
            var transform = nodes[i].attributes.transform;
            if (transform && typeof(transform) != undefined) {
                var transformValue = transform.value;
                var matrix = extractMatrix(transformValue);
                transformPoints = newPoints(points,matrix);
            } else {
                transformPoints = points;
            }            
            // console.log("value : ",transformPoints);
            if (result.hasOwnProperty(newId)){
                result[newId].push(transformPoints);
            } else {
                var array = new Array();
                result[newId] = array;
                result[newId].push(transformPoints);
            }
        }
    }
    return result;
}

/**
 * 坐标转换
 * @param {} transform 
 */
function newPoints (points,matrix) {
    var result = new Array();
    var a= matrix[0];var b= matrix[1];var c= matrix[2];
    var d= matrix[3];var e= matrix[4];var f= matrix[5];
    var num = points.length/2;

    for (let i=1;i<=num;i++) {
        var xIndex = i*2-2; var yIndex = i*2-1;
        var x = a*points[xIndex]+c*points[yIndex]+e;
        var y = b*points[xIndex]+d*points[yIndex]+f;
        result.push(x);
        result.push(y);        
    }
    return result;
}

function extractMatrix (input) {
    var result = new Array();

    var tmp;
    var reg = RegExp(/matrix/);
    tmp = input.replace(reg,'');
    reg = RegExp(/\(/);
    tmp = tmp.replace(reg,'');
    reg = RegExp(/\)/);
    tmp = tmp.replace(reg,'');
    var temp = tmp.split(',');
    for(let i=0;i<temp.length;i++){
        result.push(parseFloat(temp[i]));
    }
    return result;
}
function extractPoints (value) {
    var result = new Array();
    var tmp = value.split(' ');
    var reg = RegExp(/,/);
    for (let i=0;i<tmp.length;i++){        
        if (tmp[i].match(reg)) {
            var cood = tmp[i].split(',');
            var x = parseFloat(cood[0]);
            var y = parseFloat(cood[1]);
            result.push(x);
            result.push(y);
        }
    }
    return result;
}